﻿Imports System
Namespace libModClassVB.Classes
    Public Class clsCryptography : Implements IDisposable



        Public Function Encrypt(ByVal Plain As String) As String
            Dim Letter As String
            Dim strEncrypt As String = String.Empty

            Try
                If Plain.Trim.Length > 0 Then
                    For i = 1 To Len(Plain)
                        Letter = Mid$(Plain, i, 1)
                        Mid$(Plain, i, 1) = Chr(Asc(Letter) + 3)
                    Next
                End If

            Catch ex As Exception
                MsgBox(ex.Message + " clsEncription_clsCryptography_Encrypt")
            End Try
            Return Plain
        End Function

        'Here's the Decryption function:
        Public Function Decrypt(ByVal Encrypted As String) As String
            Dim Letter As String
            Try
                For i = 1 To Len(Encrypted)
                    Letter = Mid$(Encrypted, i, 1)
                    Mid$(Encrypted, i, 1) = Chr(Asc(Letter) - 3)
                Next i

            Catch ex As Exception
                MsgBox(ex.Message + " clsEncription_clsCryptography_Decrypt")
            End Try

            Return Encrypted
        End Function

        Public Function EncryptM2(ByVal Plain As String) As String
            Dim strEncryptm2 As String = String.Empty
            Try
                Dim cipherTest As New clsCipher
                cipherTest.set_KeyString("123")
                cipherTest.set_Text(Plain)
                cipherTest.DoXor()
                cipherTest.Stretch()
                strEncryptm2 = cipherTest.Get_Text()
            Catch ex As Exception
                MsgBox(ex.Message + " clsEncription_clsCryptography_EncryptM2")
            End Try
            Return strEncryptm2
        End Function

        Public Function DecryptM2(ByVal Encrypted As String) As String
            Dim strDecryptM2 As String = String.Empty
            Dim cipherTest As New clsCipher
            Try
                If Encrypted = String.Empty Then
                    Return String.Empty
                Else
                    cipherTest.set_KeyString("123")
                    cipherTest.set_Text(Encrypted)
                    cipherTest.DoXor()
                    cipherTest.Stretch()
                    strDecryptM2 = cipherTest.Get_Text()
                End If

            Catch ex As Exception
                MsgBox(ex.Message + " clsEncription_clsCryptography_DecryptM2")
            End Try
            Return strDecryptM2
        End Function

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace